
import 'package:bt_tool/basic/bt_controller.dart';
import 'package:bt_tool/basic/bt_language_tool.dart';
import 'package:bt_tool/basic/bt_theme_color.dart';
import 'package:bt_tool/widget/bt_base_btn_widget.dart';
import 'package:bt_tool/widget/bt_form_select_widget.dart';
import 'package:bt_tool/widget/bt_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/common/config/app_theme_color_config.dart';
import 'package:flutter_demo/tools/donext/controller/do_next_main_controller.dart';
import 'package:flutter_demo/tools/donext/do_next_define.dart';
import 'package:flutter_demo/tools/donext/model/do_next_model.dart';
import 'package:get/get.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

class DoNextMainPage extends BTStatefulWidget{

  const DoNextMainPage({super.key,super.routerConfig});

  @override
  State<StatefulWidget> createState() {
    return DoNextMainPageState();
  }

}

class DoNextMainPageState extends BTState<DoNextMainPage>{

  final DoNextMainController ctl = DoNextMainController();

  @override
  BTController btCtrl() {
    return ctl;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: const BoxConstraints.expand(),
      color: AppThemeColor.contentBgColor,
      padding: const EdgeInsets.all(10),
      child: Column(
        children: [
          const SizedBox(height: 5,),
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
                width: 160,
                height: 30,
                child: Obx(()=>BTFormSelectWidget(
                  titleStr: "当前项目：",
                  dataList: ctl.projectSelectModelList.value,
                  callback: (model){
                    ctl.selectProjectId = model.id;
                    ctl.getData();
                  },
                )),
              ),
              const SizedBox(width: 10,),
              SizedBox(
                width: 160,
                height: 30,
                child: BTFormSelectWidget(
                  titleStr: "时间范围：",
                  dataList: [
                    BTFormSelectModel(DoNextMainTimeType.today.value, "今天", true),
                    BTFormSelectModel(DoNextMainTimeType.week.value, "本周", false),
                    BTFormSelectModel(DoNextMainTimeType.all.value, "全部", false),
                  ],
                  callback: (model){
                    ctl.timeRangType.value = DoNextMainTimeType.fromValue(model.id);
                    ctl.getData();
                  },
                ),
              ),
            ],
          ),
          const SizedBox(height: 10,),
          Expanded(child: Container(
            constraints: const BoxConstraints.expand(),
            child: gridView(),
          )),
          const SizedBox(height: 10,),
          bottomActionWidget(),
        ],
      ),
    );
  }

  Widget bottomActionWidget(){
    return Row(
      children: [
        BTBtnTextWidget(
          bgColor: AppThemeColor.mainColor,
          constraints: const BoxConstraints.expand(width: 100,height: 40),
          text: "新增",
          textColor: Colors.white,
          onTap: () => ctl.create(context),
          alignment: Alignment.center,
        ),
        const SizedBox(width: 10,),
        BTBtnTextWidget(
          bgColor: AppThemeColor.mainColor,
          constraints: const BoxConstraints.expand(width: 100,height: 40),
          text: "刷新数据",
          textColor: Colors.white,
          onTap: () => ctl.getData(),
          alignment: Alignment.center,
        ),
        const SizedBox(width: 10,),
        BTBtnTextWidget(
          bgColor: AppThemeColor.mainColor,
          constraints: const BoxConstraints.expand(width: 100,height: 40),
          text: "日报排版",
          textColor: Colors.white,
          onTap: () => ctl.reportStyleClick(),
          alignment: Alignment.center,
        ),
      ],
    );
  }
  
  Widget gridView(){
    return Obx((){
      if(ctl.dataList.isEmpty){
        return Container(
          alignment: Alignment.center,
          child: Text("暂无数据".btTrans),
        );
      }
      return MasonryGridView.count(
        crossAxisCount: 2, // 列数
        mainAxisSpacing: 8, // 主轴间距
        crossAxisSpacing: 8, // 交叉轴间距
        itemCount: ctl.dataList.length, // 数据总数
        itemBuilder: (context, index) {
          DoNextModel model = ctl.dataList[index];
          return GestureDetector(
            onSecondaryTapDown: (TapDownDetails details) => ctl.showMenuView(model,details.globalPosition),
            onTap: () => ctl.detClick(model),
            child: Container(
              color: model.isSelect ? AppThemeColor.contentBgColor3 : AppThemeColor.contentBgColor2,
              padding: const EdgeInsets.all(5),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    model.content,
                    style: TextStyle(fontSize: 17, color: AppThemeColor.textColor,fontWeight: FontWeight.w600),
                  ),
                  const SizedBox(height: 4,),
                  Text(
                      "时间：${model.getTimeStr()}"
                  ),
                  const SizedBox(height: 4,),
                  Text(
                    "项目：${model.projectName.isEmpty ? "暂无" : model.projectName}",
                  ),
                  const SizedBox(height: 4,),
                  Text(
                    "状态：${model.getStatusStr()}",
                    style: TextStyle(color: model.statusTextColor()),
                  ),

                ],
              ),
            ),
          );
        },
      );
    });
  }

}

